import os
import sys

sys.path.insert(
    0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)

from unittest.mock import MagicMock, patch

import pytest

from litellm.llms.azure_ai.anthropic.transformation import AzureAnthropicConfig
from litellm.types.router import GenericLiteLLMParams


class TestAzureAnthropicConfig:
    def test_custom_llm_provider(self):
        """Test that custom_llm_provider returns 'azure_ai'"""
        config = AzureAnthropicConfig()
        assert config.custom_llm_provider == "azure_ai"

    def test_validate_environment_with_dict_litellm_params(self):
        """Test validate_environment with dict litellm_params"""
        config = AzureAnthropicConfig()
        headers = {}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = {"api_key": "test-api-key"}
        api_key = "test-api-key"

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "test-api-key"}
            result = config.validate_environment(
                headers=headers,
                model=model,
                messages=messages,
                optional_params=optional_params,
                litellm_params=litellm_params,
                api_key=api_key,
            )

            # Verify that dict was converted to GenericLiteLLMParams
            call_args = mock_validate.call_args
            assert isinstance(call_args[1]["litellm_params"], GenericLiteLLMParams)
            assert call_args[1]["litellm_params"].api_key == "test-api-key"
            assert "anthropic-version" in result

    def test_validate_environment_with_generic_litellm_params(self):
        """Test validate_environment with GenericLiteLLMParams object"""
        config = AzureAnthropicConfig()
        headers = {}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = GenericLiteLLMParams(api_key="test-api-key")
        api_key = "test-api-key"

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "test-api-key"}
            result = config.validate_environment(
                headers=headers,
                model=model,
                messages=messages,
                optional_params=optional_params,
                litellm_params=litellm_params,
                api_key=api_key,
            )

            # Verify that GenericLiteLLMParams was passed through
            call_args = mock_validate.call_args
            assert isinstance(call_args[1]["litellm_params"], GenericLiteLLMParams)
            assert "anthropic-version" in result

    def test_validate_environment_sets_api_key_in_litellm_params(self):
        """Test that api_key parameter is set in litellm_params if provided"""
        config = AzureAnthropicConfig()
        headers = {}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = {}  # Empty dict, no api_key
        api_key = "provided-api-key"

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "provided-api-key"}
            config.validate_environment(
                headers=headers,
                model=model,
                messages=messages,
                optional_params=optional_params,
                litellm_params=litellm_params,
                api_key=api_key,
            )

            # Verify that api_key was set in litellm_params
            call_args = mock_validate.call_args
            assert call_args[1]["litellm_params"].api_key == "provided-api-key"

    def test_validate_environment_converts_api_key_to_x_api_key(self):
        """Test that api-key header is converted to x-api-key (Azure Anthropic uses x-api-key)"""
        config = AzureAnthropicConfig()
        headers = {}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = {"api_key": "test-api-key"}

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "test-api-key"}
            with patch.object(
                config, "get_anthropic_headers", return_value={}
            ):
                result = config.validate_environment(
                    headers=headers,
                    model=model,
                    messages=messages,
                    optional_params=optional_params,
                    litellm_params=litellm_params,
                )

                # Verify api-key was converted to x-api-key
                assert "x-api-key" in result
                assert result["x-api-key"] == "test-api-key"
                assert "api-key" not in result

    def test_validate_environment_sets_anthropic_version(self):
        """Test that anthropic-version header is set"""
        config = AzureAnthropicConfig()
        headers = {}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = {"api_key": "test-api-key"}

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "test-api-key"}
            with patch.object(config, "get_anthropic_headers", return_value={}):
                result = config.validate_environment(
                    headers=headers,
                    model=model,
                    messages=messages,
                    optional_params=optional_params,
                    litellm_params=litellm_params,
                )

                assert result["anthropic-version"] == "2023-06-01"

    def test_validate_environment_preserves_existing_anthropic_version(self):
        """Test that existing anthropic-version header is preserved"""
        config = AzureAnthropicConfig()
        headers = {"anthropic-version": "2024-01-01"}
        model = "claude-sonnet-4-5"
        messages = [{"role": "user", "content": "Hello"}]
        optional_params = {}
        litellm_params = {"api_key": "test-api-key"}

        with patch(
            "litellm.llms.azure.common_utils.BaseAzureLLM._base_validate_azure_environment"
        ) as mock_validate:
            mock_validate.return_value = {"api-key": "test-api-key", "anthropic-version": "2024-01-01"}
            with patch.object(config, "get_anthropic_headers", return_value={"anthropic-version": "2024-01-01"}):
                result = config.validate_environment(
                    headers=headers,
                    model=model,
                    messages=messages,
                    optional_params=optional_params,
                    litellm_params=litellm_params,
                )

                assert result["anthropic-version"] == "2024-01-01"

    def test_inherits_anthropic_config_methods(self):
        """Test that AzureAnthropicConfig inherits methods from AnthropicConfig"""
        config = AzureAnthropicConfig()
        
        # Test that it has AnthropicConfig methods
        assert hasattr(config, "get_anthropic_headers")
        assert hasattr(config, "is_cache_control_set")
        assert hasattr(config, "is_computer_tool_used")
        assert hasattr(config, "transform_request")
        assert hasattr(config, "transform_response")

